{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e8b27860",
   "metadata": {},
   "source": [
    "# What\n",
    "分类任务，支持两种模式\n",
    "1. Folder模式，需要输入`train`, `valid`两个测试集对应的目录。`labels.txt`，需要训练的label，里面每个类别一行。\n",
    "2. List模式，需要输入`train`, `valid`两个测试集对应的训练文件，每行一个样本。`labels.txt`是可选参数，里面每个类别一行。`data_pattern`一个通用的目录，与train、val中的第一列进行拼接。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58acb50f",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获得视频教程\n",
    "from onekey_algo.custom.Manager import onekey_show\n",
    "onekey_show('What概览')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02dd7e75",
   "metadata": {},
   "source": [
    "### 支持的模型名称\n",
    "\n",
    "模型名称替换代码中的 `model_name`变量的值。\n",
    "\n",
    "| **模型系列** | **模型名称**                                                 |\n",
    "| ------------ | ------------------------------------------------------------ |\n",
    "| AlexNet      | alexnet                                                      |\n",
    "| VGG          | vgg11, vgg11_bn, vgg13, vgg13_bn, vgg16, vgg16_bn, vgg19_bn, vgg19 |\n",
    "| ResNet       | resnet18, resnet34, resnet50, resnet101, resnet152, resnext50_32x4d, resnext101_32x8d, wide_resnet50_2, wide_resnet101_2 |\n",
    "| DenseNet     | densenet121, densenet169, densenet201, densenet161           |\n",
    "| Inception    | googlenet, inception_v3                                      |\n",
    "| SqueezeNet   | squeezenet1_0, squeezenet1_1                                 |\n",
    "| ShuffleNetV2 | shufflenet_v2_x2_0, shufflenet_v2_x0_5, shufflenet_v2_x1_0, shufflenet_v2_x1_5 |\n",
    "| MobileNet    | mobilenet_v2, mobilenet_v3_large, mobilenet_v3_small         |\n",
    "| MNASNet      | mnasnet0_5, mnasnet0_75, mnasnet1_0, mnasnet1_3              |\n",
    "| Transformer  | ViT, SimpleViT, CrossFormer, TwinsSVT              |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e861016",
   "metadata": {},
   "source": [
    "### List模式\n",
    "\n",
    "在Onekey中List模式一般是采用labelme标注出来的结果，如果要使用自己的数据应用List模式，需要根据自己的实际情况对数据进行处理。\n",
    "\n",
    "* `train.txt`，训练数据列表，中间用\\t（Tab水平制表符）进行分割。\n",
    "* `val.txt`，验证数据列表，中间用\\t（Tab水平制表符）进行分割。\n",
    "* `labels.txt`，label的集合，表明训练数据多少标签。\n",
    "* `data_pattern`参数，所有数据存在的目录的公共前缀，如果`train.txt`,`val.txt`文件里面存放的是绝对路径，`data_pattern`设置为None即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83513dd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获得视频教程\n",
    "from onekey_algo.custom.Manager import onekey_show\n",
    "onekey_show('What概览|List')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d4380fe",
   "metadata": {},
   "source": [
    "# 需要自己配置的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a008dd0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from onekey_algo import OnekeyDS as okds\n",
    "from onekey_algo import get_param_in_cwd\n",
    "\n",
    "# model_root，模型存放位置\n",
    "model_root = get_param_in_cwd('model_root', 'models')\n",
    "# 设置为使用OKT-crop_max_roi裁剪出来的图片路径，或者你自己训练数据的路径。\n",
    "data_pattern = get_param_in_cwd('data_pattern', os.path.join(okds.ct, 'crop')) \n",
    "# model_name设置为自己训练的模型。\n",
    "model_name = get_param_in_cwd('model_name', 'resnet50')\n",
    "epoch = get_param_in_cwd('epoch', 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7050436a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from onekey_algo.classification.run_classification import main as clf_main\n",
    "from collections import namedtuple\n",
    "\n",
    "train_f = r'split_info/train.txt'\n",
    "val_f = r'split_info/val.txt'\n",
    "labels_f = r'split_info/labels.txt'\n",
    "\n",
    "params = dict(train=train_f,\n",
    "              valid=val_f,\n",
    "              labels_file=labels_f,\n",
    "              data_pattern=data_pattern,\n",
    "              j=8,\n",
    "              max2use=None,\n",
    "              val_max2use=None,\n",
    "              batch_balance=False,\n",
    "              normalize_method='imagenet',\n",
    "              model_name=model_name,\n",
    "              gpus=[0],\n",
    "              batch_size=32,\n",
    "              epochs=epoch,\n",
    "              init_lr=0.01,\n",
    "              optimizer='sgd',\n",
    "              retrain=None,\n",
    "              model_root=model_root,\n",
    "              add_date=False,\n",
    "              iters_start=0,\n",
    "              iters_verbose=128,\n",
    "              save_per_epoch=False,\n",
    "              pretrained=True)\n",
    "# 训练模型\n",
    "Args = namedtuple(\"Args\", params)\n",
    "clf_main(Args(**params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f389a016",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
